//
//  JXPVideoTableViewController.m
//  模拟-百思不得姐
//
//  Created by ColinJ on 16/8/8.
//  Copyright © 2016年 colin. All rights reserved.
//

#import "JXPVideoTableViewController.h"
#import "UIViewController+MMDrawerController.h"
#import "AFNetworking.h"
#import "SVProgressHUD.h"
#import "CCYMeSubscribModel.h"
#import "CCYMeNewTableViewCell.h"
#import "JXPRecommendTableViewCell.h"
#import "JXPRecommendModel.h"
#import "JXPMoviePlayer.h"


@interface JXPVideoTableViewController ()

@property (nonatomic,strong)NSMutableArray *modelArray;
//第二次请求的时候需要传入的参数
@property(nonatomic,copy)NSString *maxtime;
@property (nonatomic,assign)NSInteger page;


@end

@implementation JXPVideoTableViewController

- (NSMutableArray *)modelArray
{
    if (!_modelArray) {
        _modelArray = [[NSMutableArray alloc] init];
    }
    return _modelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self fistRequestData];
    [self pullDownRefresh];
}

- (void)setupUI{
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" selectImage:@"MainTagSubIconClick" target:self action:@selector(btnClick)];
    self.view.backgroundColor = XMGVColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"CCYMeNewTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"JXPRecommendTableViewCell" bundle:nil] forCellReuseIdentifier:@"JXPcell"];

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 10;


}

- (void)btnClick
{
    [self.mm_drawerController toggleDrawerSide:(MMDrawerSideLeft) animated:YES completion:nil];
}

- (void)pullDownRefresh{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 0;
        [self requestDataWithMaxTime];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page = self.page + 1;
        [self requestDataWithMaxTime];
    }];
}

//第一次数据请求
- (void)fistRequestData{
    //帖子的类型，1为全部 10为图片 29为段子 31为音频 41为视频
    NSDictionary *dic = @{@"a" : @"list", @"c" : @"data",@"type":@41,@"per":@10};
    NSString *url = @"http://api.budejie.com/api/api_open.php";
    [[AFHTTPSessionManager manager] GET:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dict = responseObject[@"info"];
        self.maxtime = dict[@"maxtime"];
        for (NSDictionary *tempDict in responseObject[@"list"]){
            JXPRecommendModel *model = [[JXPRecommendModel alloc]init];
            [model setValuesForKeysWithDictionary:tempDict];
            [self.modelArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"活动加载失败，请检查网络"];
    }];
}

//刷新请求
- (void)requestDataWithMaxTime{
    NSDictionary *dic = @{@"a" : @"newlist", @"c" : @"data",@"maxtime":_maxtime,@"page": @(_page),@"type":@41,@"per":@10};
    NSString *url = @"http://api.budejie.com/api/api_open.php";
    [[AFHTTPSessionManager manager] GET:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dict = responseObject[@"info"];
        if (_page == 0) {
            [self.modelArray removeAllObjects];
        }
        self.maxtime = dict[@"maxtime"];
        for (NSDictionary *tempDict in responseObject[@"list"]){
            JXPRecommendModel *model = [[JXPRecommendModel alloc]init];
            [model setValuesForKeysWithDictionary:tempDict];
            [self.modelArray addObject:model];
        }
        [self.tableView.mj_footer endRefreshing];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"活动加载失败，请检查网络"];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 JXPRecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JXPcell" forIndexPath:indexPath];
  JXPRecommendModel*model = _modelArray[indexPath.row];
    [cell setupDataWithModel:model];
   return cell;

}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
